import request from '@/utils/requets'
import { ReturnRoles, Role, ReturnQx } from './type'

enum API {
    //获取全部的职位接口
    ALLROLE_URL = '/admin/acl/role/',
    //新增岗位的接口地址
    ADDROLE_URL = '/admin/acl/role/save',
    //更新已有的职位
    UPDATEROLE_URL = '/admin/acl/role/update',
    //获取全部的菜单与按钮的数据
    ALLPERMISSTION = '/admin/acl/permission/toAssign/',
    //给相应的职位分配权限
    SETPERMISTION_URL = '/admin/acl/permission/doAssign/?',
    //删除已有的职位
    REMOVEROLE_URL = '/admin/acl/role/remove/',
}
// 获取全部的职位接口
export const getRole = (pageNO: number, pageSize: number, roleName: string) =>
    request.get<any, ReturnRoles>(
        API.ALLROLE_URL + `${pageNO}/${pageSize}/?roleName=${roleName}`,
    )

// 新增或编辑角色
export const addOrUpdateRole = (data: Role) => {
    if (data.id) {
        return request.put<any, any>(API.UPDATEROLE_URL, data)
    } else {
        return request.post<any, any>(API.ADDROLE_URL, data)
    }
}
// 删除角色
export const removeRole = (roleId: number) =>
    request.delete<any, any>(API.REMOVEROLE_URL + roleId)

// 获取权限
export const toAssign = (roleId: number) =>
    request.get<any, ReturnQx>(API.ALLPERMISSTION + roleId)

// 分配权限
export const doAssign = (roleId: number, permissionId: number[]) =>
    request.post<any, any>(
        API.SETPERMISTION_URL + `roleId=${roleId}&permissionId=${permissionId}`,
    )
